const app = getApp();
Component({
  options: {
    addGlobalClass: true,
  },
  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached: function () { }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready: function() {
    this.getActivitys();
  },
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    list: [],
    pageNum: 1,
    isRefresh: true,
  },
  methods: {
    toChild(e) {
      wx.navigateTo({
        url: '/pages/activitys/detail/detail?id=' + e.currentTarget.dataset.id
      })
    },

    loadActivitys(pageNum){
      let _that = this;
      let _acs = this.data.list;
      if(_that.data.isRefresh){
        wx.showLoading({
          title: '加载中',
        });
        let _url = app.globalData.apiServerUrl + '/list?page=' + pageNum;
        wx.request({
          url: _url,
          success(res) {
            wx.hideLoading();
            let _d = res.data.data;
            if(_d && _d.length > 0){
              _acs.push.apply(_acs, _d);
              _that.setData({
                list: _acs,
                pageNum: pageNum
              });
            }else{
              _that.showToast('我已经露底啦(✿◡‿◡)');
              _that.setData({
                isRefresh: false
              });
            }
          }
        })
      } else {
        _that.showToast('我是有底线的(✿◡‿◡)');
      }
      
    },

    /**
     * 获取活动列表
     */
    getActivitys() {
      this.loadActivitys(this.data.pageNum);
    },

    // 滚动到列表底部
    goBottom(e) {
      this.loadActivitys(this.data.pageNum+1);
    },




    scroll(e) {
      console.log(e)
    },

    showToast(msg, type, time) {
      if(!time){
        time = 5000;
      }
      if(!type){
        type = 'info';
      }
      wx.showToast({
        title: msg,
        icon: 'success_no_circle',
        duration: time
      });
    }

  }
});